/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aljfce.business.event.boundary;

import com.aljfce.business.entity.Event;
import com.aljfce.business.entity.Notification;
import com.aljfce.business.event.control.EventManager;
import com.aljfce.business.profile.boundary.UserHome;
import com.aljfce.business.profile.control.ProfileManager;
import com.aljfce.business.profile.exception.UserNotFoundException;
import java.io.Serializable;
import java.security.Principal;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.Stateful;
import javax.ejb.Stateless;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Jacopo Ferrigno <jacopo.ferrigno@gmail.com>
 */
@ManagedBean
@ViewScoped
public class OwnEvent{

   
    @Inject
    EventManager evm;
    @Inject
    ProfileManager pm;
    
    Notification notification;
    
    
        

    Principal principal;

    private Event eventToDelete;

    public Notification getNotification() {
        return notification;
    }

    public void setNotification(Notification notification) {
        this.notification = notification;
    }

    
    
    
    /**
     * Get the value of eventToDelete
     *
     * @return the value of eventToDelete
     */
    public Event getEventToDelete() {
        return eventToDelete;
    }

    /**
     * Set the value of eventToDelete
     *
     * @param eventToDelete new value of eventToDelete
     */
    public void setEventToDelete(Event eventToDelete) {
        this.eventToDelete = eventToDelete;
    }

    @PostConstruct
    public void init() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        principal = request.getUserPrincipal();
        

    }

    public void edit(Event event) {
        //TODO write real implementation
        
    }



    public void delete(Event event) {

        //FIXME do something when this call fails
        
        if (event != null) {
            try {
                evm.deleteEvent(event, pm.getProfile(principal.getName()));//Manage list of guests then maybe add it as parameter to deleteEvent as updateEvent!!!
            } catch (UserNotFoundException ex) {
                Logger.getLogger(OwnEvent.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }


}
